package me.longerian.abc.chapter4;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.util.Arrays;

import me.longerian.abc.chapter3.vote.VoteMsg;
import me.longerian.abc.chapter3.vote.VoteMsgTextCoder;

public class VoteMulticastReceiver {

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		InetAddress address = InetAddress.getByName("224.0.0.1"); // Multicast
																// address
		if (!address.isMulticastAddress()) { // Test if multicast address
			throw new IllegalArgumentException("Not a multicast address");
		}

		int port = 9001; // Multicast port
		MulticastSocket sock = new MulticastSocket(port); // for receiving
		sock.joinGroup(address); // Join the multicast group

		VoteMsgTextCoder coder = new VoteMsgTextCoder();

		// Receive a datagram
		DatagramPacket packet = new DatagramPacket(
				new byte[VoteMsgTextCoder.MAX_WIRE_LENGTH],
				VoteMsgTextCoder.MAX_WIRE_LENGTH);
		sock.receive(packet);

		VoteMsg vote = coder.fromWire(Arrays.copyOfRange(packet.getData(), 0,
				packet.getLength()));

		System.out.println("Received Text-Encoded Request ("
				+ packet.getLength() + " bytes): ");
		System.out.println(vote);

		sock.close();
	}

}
